"""
@File    : server.py
@Author  : qve-wzy
@Time    : 2020/9/3 0003 9:27
"""
import threading
import time
from util.dos_CMD import DosCmd
from util.judgmentPortStatus import JudgmentPortStatus
from util.wirteUserCommand import WriteUserCommand


class Server:
    '''
    获取设备信息
    '''

    def __init__(self):
        '''
        初始化
        '''
        self.dos = DosCmd()
        self.device_list = self.get_devices()
        self.write_file = WriteUserCommand()

    def get_devices(self):
        '''
        获取设备信息
        :return:
        '''
        list = []
        result_list = self.dos.excute_cmd_result('adb devices')
        if  len(result_list) >=2:
        # 就算设备不存在也会输出一行List of devices attached
        # 所以如果有一个连接设备，那么就至少会有2行数据
            for i in result_list:
                if 'List' in i: # 判定是不是List of devices attached 这一行数据
                    continue
                dev_info = i.split('\t')
                if dev_info[1] == 'device':# 判断是不是设备信息
                    list.append(dev_info[0]) #如果是模拟器则获取IP+PORT的连接地址，如果是真手机则获取：手机连接设备信息
                    print(list)
            return list
        else:
            return None

    def create_port_data(self,start):
        '''
        创建可用的端口号
        :return:
        '''
        create_port = JudgmentPortStatus()
        list = []
        list = create_port.create_port_list(start,self.device_list)
        return list

    def create_DOS_command(self,i):
        '''
        端口号存在，生成对应的DOS命令
        :return:
        '''
        # appium -p 4700 -bp 4701 -U 127.0.0.1:21503
        command_list = []
        appium_port_list = self.create_port_data(4700)
        bootstrap_port_list = self.create_port_data(4900)
        devices_list = self.device_list
        # appium -p 4723 -bp 4726 -U 127.0.0.1:62001 --no-reset --session-override --log 绝对lujing
        command = "appium -p "+str(appium_port_list[i])+" -bp "+str(bootstrap_port_list[i])+" -U "+devices_list[i]+" --no-reset --session-override --log C:/Users/Administrator/PycharmProjects/appnium-test/log/test01.log"
        command_list.append(command)
        self.write_file.write_data(i,
                                   devices_list[i],
                                   str(bootstrap_port_list[i]),
                                   str(appium_port_list[i]))
        return command_list

    def start_server(self,i):
        '''
        执行DOS命令，启动服务
        :param i: 服务ID
        :return:
        '''
        self.start_list = self.create_DOS_command(i)
        print('start_list-->',self.start_list)
        self.dos.excute_cmd(self.start_list[0])

    def kill_server(self):
        '''
        关闭服务
        :param i: 服务ID
        :return:
        '''
        server_list = self.start_list = self.dos.excute_cmd_result('tasklist | find "node.exe"')
        if len(server_list)>0:# 如果服务存在
            self.dos.excute_cmd('taskkill -F -PID node.exe') # 关闭服务

    def main(self):
        '''
        主函数入口
        :return:
        '''
        thread_list = []
        self.kill_server()
        self.write_file.clear_data()
        for i in range(len(self.device_list)):
            appium_start = threading.Thread(target=self.start_server,args=(i,))
            thread_list.append(appium_start)
        for j in thread_list:
            j.start() # 开启多线程服务
        time.sleep(30)

if __name__ == '__main__':
    server = Server()
    print(server.main())